<?php
/* SVN FILE: $Id: item_randommoney.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Random Money item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/random/item_randommoney.php $
*/

class item_randommoney extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Get Random money";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Get random money from {low} to {high}";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * The description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $itemDescription;
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
			
			$this->item['item_name'] = str_replace(array('{low}','{high}'),array($this->itemSettings['lowest'],$this->itemSettings['highest']),$this->item['item_name']);
		}
		else
		{
			$this->itemSettings['lowest'] = 0;
			$this->itemSettings['highest'] = 1000;
		}
		
		$this->itemDescription = str_replace(array('{low}','{high}'),array($this->itemSettings['lowest'],$this->itemSettings['highest']),$this->item['item_description']);
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['min'] = marketFormatter::currencyFormat(ipsRegistry::getClass('output')->formSimpleInput('item[lowest]', $this->itemSettings['lowest'], 10));
		$form['max'] = marketFormatter::currencyFormat(ipsRegistry::getClass('output')->formSimpleInput('item[highest]', $this->itemSettings['highest'], 10));
		return <<<EOF
				<li>
					<label>{$this->lang->words['tags_for_description']}</label>
					<div style='display: inline-block'>
						{low} = {$this->lang->words['desc_min_money']}
						<br />
						{high} = {$this->lang->words['desc_max_money']}
						<br />
						{$this->lang->words['tags_settable']}
					</div>
				</li>
				<li>
					<label>{$this->lang->words['min_money']}</label>
					{$form['min']}
				</li>
				<li>
					<label>{$this->lang->words['max_money']}</label>
					{$form['max']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @todo
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['lowest'] = numbers::parseInt($input['lowest']);
		$setting['highest'] = numbers::parseInt($input['highest']);
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		$amount = numbers::rand($this->itemSettings['lowest'], $this->itemSettings['highest']);
		marketRegistry::recordReward($amount);
		$this->writeLog($invItem, "Got ".$amount." from random money");
		
		IPSMember::save( $this->memberData['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::add($this->memberData[$this->settings['market_members_field']], $amount) ) ) );
		
		$this->lang->words['gain_x_money'] = sprintf($this->lang->words['gain_x_money'], marketFormatter::formatCurrency($amount));
		
		$this->deleteItems($invItem);
		
		$this->registry->getClass('output')->redirectScreen( $this->lang->words['gain_x_money'], $this->settings['base_url_with_app'] . "module=inventory" );
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
	
	/**
	 * Get the description of the item, normally called when there is no intentory item being used
	 *
	 * @access	public
	 * @return	string
	 */
	public function parseStoreDescription()
	{
		return $this->itemDescription;
	}
	
	/**
	 * Get the description of the item, normally called when there is one intentory items being used
	 *
	 * @access	public
	 * @param	array	$invItem	The inventory item information
	 * @return	string
	 */
	public function parseInventoryDescription(array $invItem)
	{
		return $this->itemDescription;
	}
}
